package com.terrapages.wmsc4j.example;

import java.awt.Dimension;
import java.net.MalformedURLException;
import java.net.URL;

import com.terrapages.wmsc4j.bean.BBox;
import com.terrapages.wmsc4j.bean.Pos;
import com.terrapages.wmsc4j.cache.Cache;
import com.terrapages.wmsc4j.cache.WMSCache;
import com.terrapages.wmsc4j.layer.StaticLayer;
import com.terrapages.wmsc4j.layer.profiles.GlobalProfile;
import com.terrapages.wmsc4j.layer.resolution.Resolution;
import com.terrapages.wmsc4j.layer.resolution.ResolutionSet;
import com.terrapages.wmsc4j.layer.tile.ImageMimeType;
import com.terrapages.wmsc4j.layer.tile.TileFormat;
import com.terrapages.wmsc4j.wms.WMSParameters;
import com.terrapages.wmsc4j.wms.WMSService;

public class LayerCreation {
	//The URL of the WMS service
	private static final String WMS_URL = "http://wmsc1.terrapages.net/getdevmap";
	//A query that works which WMSC4j will use to create other requests
	private static final String WMS_QUERY = 
		"LAYERS=UnprojectedStreet&" +
		"FORMAT=image/jpeg&" +
		"SERVICE=WMS&" +
		"VERSION=1.1.1&" +
		"REQUEST=GetMap&" +
		"STYLES=&" +
		"EXCEPTIONS=application/vnd.ogc.se_inimage&" +
		"SRS=EPSG:4326&" +
		"BBOX=138.515625,-34.980469,138.691406,-34.804687&" +
		"WIDTH=256&" +
		"HEIGHT=256";
	
	protected static StaticLayer theHardWay() throws MalformedURLException
	{
//		Static layer is the simplest form of layer
		final StaticLayer layer = new StaticLayer( 
				//See the getCache method for how to make a cache
				getCache(),
				
				//What resolutions (zooms) does this layer support
				//You will find most layers use this set of resolutions
				new ResolutionSet(
					//Starting from this resolution
					new Resolution(0.703125),
					//divide by 2 until we have this many resolutions
					20
				),
				
				//The format of each tiles image
				new TileFormat (
						//The size, in pixels, of the tiles 
						new Dimension(256,256), 
						//The type of image the tile is. 
						ImageMimeType.JPEG
				),
				
				//Basicly what coverage area does the layer cover. Usually the whole earth.
				new BBox(-90,-180,90,180),
				
				//This is a reference point at which all tiles begin.
				//Usually at the most south-western point of the coverage
				new Pos(-90,-180),
				
				/*This parameter is not actually used via WMS 
				because it is already defined in the WMS parameters
				and it will not change. It could be just null but we'll 
				name the layer for the sake of it*/
				"UnprojectedStreet"
		);
		
		return layer;
	}
	
	protected static StaticLayer theEasyWay()
	{
		// Makes an UNPROJECTED Global Profile layer
		//http://wiki.osgeo.org/index.php/WMS_Tiling_Client_Recommendation#Unprojected_Profile
		final StaticLayer layer = GlobalProfile.UNPROJECTED
			.makeStaticLayer(
				//See the getCache method for how to make a cache
				getCache(),
				//How many resolutions are there
				20, 
				//What kind of images does the layer use
				ImageMimeType.JPEG,
				//Whats the name of the layer
				"UnprojectedStreet"
			);
		
		return layer;
	}
	
	
	/**
	 * You need a cache for the layer for the easy way and the hard way
	 * This method pus that cache making code in one place the cache.
	 * @return
	 */
	private static Cache getCache()
	{
		WMSCache cache = null;
		try {
			
			
//			WMSCache means that you connect to this layers cache via WMS
			cache =  new WMSCache(
				//The WMS cache will connect to this particulat WMS service
				new WMSService(
						//Use this WMS service (Host and Path)
						new URL(WMS_URL),
						//make a WMSParameters object this way
						WMSParameters.parseQuery(WMS_QUERY)
				)
			);
			
			
			
			
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return cache;
	}
}
